package com.quanyan.ic.datacenter.dao.impl;

import com.quanyan.ic.datacenter.constant.Constants;
import com.quanyan.ic.datacenter.dao.AlarmsHandelReportPlaceDao;
import com.quanyan.ic.datacenter.http.req.ReqHandleAlarm;
import com.quanyan.ic.datacenter.http.req.ReqReport;
import com.quanyan.ic.datacenter.http.resp.RespAlarmsHandlePie;
import com.quanyan.ic.datacenter.mapper.icData.AlarmsHandelReportPlaceMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmsHandelReportPlace;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmsHandelReportPlaceExample;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jian.zhou on 2017/8/23.
 */
@Repository
public class AlarmsHandelReportPlaceDaoImpl implements AlarmsHandelReportPlaceDao {

    @Autowired
    private AlarmsHandelReportPlaceMapper alarmsHandelReportPlaceMapper;

    @Override
    public List<RespAlarmsHandlePie> findAlarmsHandleCityPie(ReqReport reqReport) {
        AlarmsHandelReportPlaceExample alarmsHandelReportPlaceExample = new AlarmsHandelReportPlaceExample();
        AlarmsHandelReportPlaceExample.Criteria criteria = alarmsHandelReportPlaceExample.createCriteria();
        if (reqReport.getStartDate().equals(reqReport.getEndDate()))
            criteria.andDateEqualTo(reqReport.getStartDate());
        else{
            criteria.andDateGreaterThanOrEqualTo(reqReport.getEndDate());
            criteria.andDateLessThanOrEqualTo(reqReport.getStartDate());
        }
        criteria.andCityIdEqualTo(reqReport.getCityId());
        criteria.andTypeEqualTo(Constants.AlarmsType.alarms_type_video);
        List<AlarmsHandelReportPlace> cityList =  alarmsHandelReportPlaceMapper.selectByExample(alarmsHandelReportPlaceExample);
        List<RespAlarmsHandlePie> result = new ArrayList<>();
        if ( cityList != null && cityList.size() > 0 ){
            for (AlarmsHandelReportPlace db : cityList){
                RespAlarmsHandlePie respAlarmsHandlePie = new RespAlarmsHandlePie();
                respAlarmsHandlePie.setId(db.getPlaceId());
                respAlarmsHandlePie.setType(db.getType());
                respAlarmsHandlePie.setRemark(db.getHandleRemark());
                respAlarmsHandlePie.setCount(db.getCount());
                result.add(respAlarmsHandlePie);
            }
        }
        return result;
    }


    public void insert(PlaceUnitInfo placeUnitInfo,ReqHandleAlarm reqHandleAlarm){
        AlarmsHandelReportPlaceExample example = new AlarmsHandelReportPlaceExample();
        AlarmsHandelReportPlaceExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeUnitInfo.getPlaceId());
        criteria.andHandleEqualTo(reqHandleAlarm.getType());
        criteria.andDateEqualTo(new Date());
        List<AlarmsHandelReportPlace> cityList =  alarmsHandelReportPlaceMapper.selectByExample(example);
        if ( cityList != null && cityList.size() > 0 ){
            AlarmsHandelReportPlace db = cityList.get(0);
            AlarmsHandelReportPlace update = new AlarmsHandelReportPlace();
            update.setId(db.getCityId());
            update.setCount(db.getCount()+1);
            alarmsHandelReportPlaceMapper.updateByPrimaryKeySelective(update);
        }else {
            AlarmsHandelReportPlace insert = new AlarmsHandelReportPlace();
            insert.setCityId(placeUnitInfo.getCityId());
            insert.setCityName(placeUnitInfo.getCityName());
            insert.setPlaceId(placeUnitInfo.getPlaceId());
            insert.setPlaceName(placeUnitInfo.getPlaceName());
            insert.setDate(new Date());
            insert.setCount(1);
            insert.setHandle(reqHandleAlarm.getType());
            insert.setHandleRemark(reqHandleAlarm.getRemark());
            alarmsHandelReportPlaceMapper.insertSelective(insert);
        }

    }


}
